package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.dto.UmsMenuNode;
import cn.zcscloud.mall.mbg.model.UmsMenu;
import cn.zcscloud.mall.service.UmsMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 添加后台菜单
     *
     * @param umsMenu
     * @return
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody UmsMenu umsMenu) throws ParseException {
        //设置菜单级数
        if (umsMenu.getParentId() == 0) {
            umsMenu.setLevel(0);
        }else {
            umsMenu.setLevel(1);
        }
        //获取当前时间并设置上
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Date creatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(nowTime);
        umsMenu.setCreateTime(creatTime);
        umsMenuService.save(umsMenu);
        return R.success(null);
    }

    /**
     * 根据ID删除后台菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") Integer id) {
        umsMenuService.deleteById(id);
        return R.success(null);
    }

    /**
     * 分页模糊查询后台菜单
     *
     * @return
     */
    @GetMapping("list/{parentId}")
    public R<PageRequest> list(@PathVariable("parentId") Integer parentId, Integer pageNum, Integer pageSize) {
        //分页构造器
        IPage<UmsMenu> iPage = new Page<>(pageNum, pageSize);
        //查询构造器
        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(parentId != null, UmsMenu::getParentId, parentId);
        lqw.orderByDesc(UmsMenu::getSort);
        //查询构造并返回
        umsMenuService.page(iPage, lqw);
        PageRequest pr = PageRequest.init(iPage);
        return R.success(pr);
    }

    /**
     * 树形结构返回所有菜单列表
     *
     * @return
     */
    @GetMapping("/treeList")
    public R<List<UmsMenuNode>> treeList() throws InvocationTargetException, IllegalAccessException {
        List<UmsMenuNode> list = umsMenuService.treeList();
        for (UmsMenuNode umsMenuNode : list) {
            System.out.println(umsMenuNode);
        }
        return R.success(list);
    }

    /**
     * 修改后台菜单
     *
     * @param umsMenu
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> update(@RequestBody UmsMenu umsMenu, @PathVariable("id") Integer id) {
        umsMenu.setId(id.longValue());
        umsMenuService.updateById(umsMenu);
        return R.success(null);
    }

    /**
     * 修改菜单显示状态
     *
     * @param id
     * @return
     */
    @PostMapping("/updateHidden/{id}")
    public R<String> updateHidden(@PathVariable("id") Integer id, Integer hidden) {
        UpdateWrapper<UmsMenu> uw = new UpdateWrapper<>();
        uw.lambda().eq(UmsMenu::getId, id);
        uw.lambda().set(UmsMenu::getHidden, hidden);
        umsMenuService.update(uw);
        return R.success(null);
    }

    /**
     * 根据ID获取菜单详情
     *
     * @return
     */
    @GetMapping("/{id}")
    public R<UmsMenu> findById(@PathVariable("id") Integer id) {
        UmsMenu umsMenu = umsMenuService.getById(id);
        return R.success(umsMenu);
    }
}
